home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / vim / src / linefunc.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  3KB  |  164 lines

  1. /* vi:ts=4:sw=4
  2.  *
  3.  * VIM - Vi IMproved        by Bram Moolenaar
  4.  *
  5.  * Read the file "credits.txt" for a list of people who contributed.
  6.  * Read the file "uganda.txt" for copying and usage conditions.
  7.  */
  8.  
  9. /*
  10.  * linefunc.c: some functions to move to the next/previous line and
  11.  *               to the next/previous character
  12.  */
  13.  
  14. #include "vim.h"
  15. #include "globals.h"
  16. #include "proto.h"
  17.  
  18. /*
  19.  * coladvance(col)
  20.  *
  21.  * Try to advance the Cursor to the specified column.
  22.  */
  23.  
  24.     void
  25. coladvance(wcol)
  26.     colnr_t         wcol;
  27. {
  28.     int                 index;
  29.     register char_u        *ptr;
  30.     register colnr_t    col;
  31.  
  32.     ptr = ml_get(curwin->w_cursor.lnum);
  33.  
  34.     /* try to advance to the specified column */
  35.     index = -1;
  36.     col = 0;
  37.     while (col <= wcol && *ptr)
  38.     {
  39.         ++index;
  40.         /* Count a tab for what it's worth (if list mode not on) */
  41.         col += chartabsize(*ptr, (long)col);
  42.         ++ptr;
  43.     }
  44.     /*
  45.      * in insert mode it is allowed to be one char beyond the end of the line
  46.      */
  47.     if ((State & INSERT) && col <= wcol)
  48.         ++index;
  49.     if (index < 0)
  50.         curwin->w_cursor.col = 0;
  51.     else
  52.         curwin->w_cursor.col = index;
  53. }
  54.  
  55. /*
  56.  * inc(p)
  57.  *
  58.  * Increment the line pointer 'p' crossing line boundaries as necessary.
  59.  * Return 1 when crossing a line, -1 when at end of file, 0 otherwise.
  60.  */
  61.     int
  62. inc_cursor()
  63. {
  64.     return inc(&curwin->w_cursor);
  65. }
  66.  
  67.     int
  68. inc(lp)
  69.     register FPOS  *lp;
  70. {
  71.     register char_u  *p = ml_get_pos(lp);
  72.  
  73.     if (*p != NUL)
  74.     {            /* still within line */
  75.         lp->col++;
  76.         return ((p[1] != NUL) ? 0 : 1);
  77.     }
  78.     if (lp->lnum != curbuf->b_ml.ml_line_count)
  79.     {            /* there is a next line */
  80.         lp->col = 0;
  81.         lp->lnum++;
  82.         return 1;
  83.     }
  84.     return -1;
  85. }
  86.  
  87. /*
  88.  * incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
  89.  */
  90.     int
  91. incl(lp)
  92.     register FPOS *lp;
  93. {
  94.     register int r;
  95.  
  96.     if ((r = inc(lp)) == 1 && lp->col)
  97.         r = inc(lp);
  98.     return r;
  99. }
  100.  
  101. /*
  102.  * dec(p)
  103.  *
  104.  * Decrement the line pointer 'p' crossing line boundaries as necessary.
  105.  * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
  106.  */
  107.     int
  108. dec_cursor()
  109. {
  110.     return dec(&curwin->w_cursor);
  111. }
  112.  
  113.     int
  114. dec(lp)
  115.     register FPOS  *lp;
  116. {
  117.     if (lp->col > 0)
  118.     {            /* still within line */
  119.         lp->col--;
  120.         return 0;
  121.     }
  122.     if (lp->lnum > 1)
  123.     {            /* there is a prior line */
  124.         lp->lnum--;
  125.         lp->col = STRLEN(ml_get(lp->lnum));
  126.         return 1;
  127.     }
  128.     return -1;                    /* at start of file */
  129. }
  130.  
  131. /*
  132.  * decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
  133.  */
  134.     int
  135. decl(lp)
  136.         register FPOS *lp;
  137. {
  138.         register int r;
  139.  
  140.         if ((r = dec(lp)) == 1 && lp->col)
  141.                 r = dec(lp);
  142.         return r;
  143. }
  144.  
  145. /*
  146.  * make sure curwin->w_cursor in on a valid character
  147.  */
  148.     void
  149. adjust_cursor()
  150. {
  151.     int len;
  152.  
  153.     if (curwin->w_cursor.lnum == 0)
  154.         curwin->w_cursor.lnum = 1;
  155.     if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
  156.         curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
  157.  
  158.     len = STRLEN(ml_get(curwin->w_cursor.lnum));
  159.     if (len == 0)
  160.         curwin->w_cursor.col = 0;
  161.     else if (curwin->w_cursor.col >= len)
  162.         curwin->w_cursor.col = len - 1;
  163. }
  164.